package com.sz.pc.utils.taojin;

import com.sz.pc.utils.GV;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.sz.pc.utils.GV.i;

public class ShuangSeQiuUtil {
    /**
     * 对比中奖结果
     */

    /**
     * 复式投注拆分
     */

    /**
     * 胆拖拆分
     */


    /**
     * 【普通投注机选】
     */
    public static List<String> getPTJX(int count) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            String jx = getYZHM();
            System.out.println(jx);
            list.add(jx);
        }
        return list;
    }

    /**
     * 产生一组双色球选号
     *
     * @return 一注号码
     */
    private static String getYZHM() {
        //33选6
        int[] exist = new int[6]; //默认全0
        for (int i = 0; i < 6; i++) {
            int ball = getABall(33, exist);
            exist[i] = ball; //暂存已选的球
        }
        sort(exist); //排序
        //16选1
        int specialBall = getABall(16, null);

        //拼接字符串
        String hm = "";
        for (int i = 0; i < 6; i++) {
            String num = exist[i] < 10 ? "0" + exist[i] : "" + exist[i];
            if (i == 0) {
                hm += num;
            } else {
                hm += "," + num;
            }
        }
//        hm += "\t"+(specialBall<10? "0"+specialBall:""+specialBall);
        hm += "_" + (specialBall < 10 ? "0" + specialBall : "" + specialBall);
        return hm;
    }

    /**
     * 摇出一个号码
     *
     * @param total 球总数
     * @param exist 已经选出的球
     * @return 一个新号码
     */
    private static int getABall(int total, int[] exist) {
        Random random = new Random();
        int ball = random.nextInt(total) + 1;
        while (true) {
            if (contains(exist, ball)) {
                ball = random.nextInt(total) + 1;
            } else {
                break; //取到了新球，结束
            }
        }
        return ball;
    }

    /**
     * 判断数组是否包含某个元素
     *
     * @param array 数组
     * @param value 元素
     * @return 是否存在
     */
    private static boolean contains(int[] array, int value) {
        boolean c = false;
        if (array == null) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i] == value) {
                c = true;
                break;
            }
        }
        return c;
    }

    /**
     * 排序
     *
     * @param array 数组
     */
    private static void sort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) { //比多少次
            for (int j = i + 1; j < array.length; j++) { //每次循环，将最小数提前
                if (array[i] > array[j]) { //小数冒泡
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    /**
     * 【判断中奖结果】
     */
    public static int hitLottery(String buy, String jackPot) {
        int rNum = 0;   //红色命中个数
        int bNum = 0;   //蓝色命中个数
        int result = 0; //中奖结果（0为未中奖，1表示一等奖）
        String[] buyRAndB = buy.split("_");
        String[] jackPotRAndB = jackPot.split("_");
        //判断蓝球是否相等
        if (buyRAndB[1].equals(jackPotRAndB[1])) {
            bNum++;
        }
        int[] rBuy = getRed(buyRAndB[0]);
        int[] rJackPot = getRed(jackPotRAndB[0]);
        for(int i=0;i<rJackPot.length;i++){
            for(int j=0;j<rBuy.length;j++){
                if(rJackPot[i]==rBuy[j]){
                    rNum++;
                }
            }
        }

        if(rNum==6 && bNum==1){
            result = 1 ;
        }else if (rNum==6) {
            result = 2 ;
        }else if (rNum==5 && bNum==1) {
            result = 3 ;
        }else if (rNum==5 ||(rNum==4&&bNum==1)) {
            result = 4 ;
        }else if (rNum==4 ||(rNum==3&&bNum==1)) {
            result = 5 ;
        }else if (bNum==1) {
            result = 6 ;
        }
         return result;
    }

    private static int[] getRed(String redStr) {
        String[] red = redStr.split(",");
        int[] r = {GV.i(red[0]),GV.i(red[1]),GV.i(red[2]),GV.i(red[3]),GV.i(red[4]),GV.i(red[5])};
        return r;
    }

    public static void main(String[] args) {
        //---------- 1，机选5注测试 ----------
//        int arrayNums = 5; //机选5组
//        System.out.println(getPTJX(5).toString());

        //---------- 2,，对比中奖结果测试 ----------
        String buy = "01,02,10,12,18,33_16";
        String jackPot = "03,04,10,12,18,31_11";
        System.out.println(hitLottery(buy,jackPot));
    }
}
